package zj.chunan.navigate.action;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import zj.chunan.navigate.model.Navigate;
import zj.chunan.navigate.service.NavigateManager;

public class NavigateSaveAction extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		NavigateManager navigateManager = new NavigateManager();
		Navigate obj = null;
		request.setCharacterEncoding("UTF-8");
		String id = request.getParameter("id");
		String number = request.getParameter("number");
		String parentId = request.getParameter("parentId");
		String leaf = request.getParameter("leaf");
		String title = request.getParameter("title");
		String url = request.getParameter("url");

		if (null != id && !"".equals(id)) {
			obj = navigateManager.get(id);
			if (obj == null) {
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("/navigate/error.jsp");
				dispatcher.forward(request, response);
				return;
			}
		} else {
			obj = new Navigate();
			obj.setLeaf(new Integer(leaf));
			obj.setParentId(new Integer(parentId));
		}
		obj.setNumber(new Integer(number));
		obj.setTitle(title);
		obj.setUrl(url);
		if (null != id && !"".equals(id)) {
			navigateManager.update(obj);
		} else {
			navigateManager.save(obj);
		}
		RequestDispatcher dispatcher = request
				.getRequestDispatcher("/navigate/success.jsp");
		dispatcher.forward(request, response);
	}
}
